//
//  BRZeroTableViewCell.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/9/28.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRZeroTableViewCell.h"
#import "BRDetailOneTableViewCell.h"
#import "BRDetailTwoTableViewCell.h"

@implementation BRZeroTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    self.tableView.estimatedRowHeight = 72;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.tableView.backgroundColor = UIColor.qd_backgroundColorLighten2;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BRDetailOneTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"BRDetailOneTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BRDetailTwoTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"BRDetailTwoTableViewCell"];

    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}

- (void)setDto:(BROrderDetailModel *)dto {
    _dto = dto;
    self.state  = [self getStateInModel:dto];
    
    [self.tableView reloadData];
}

- (NSInteger)getStateInModel:(BROrderDetailModel *)dto {
    
    if (dto.paymentState == 2) {
        //免费
        return 2;
    }
    
    if (dto.state == 3 || dto.state  == 7 ) {
        //报失 或完成
//        return 1;
        return 2;
    }
    if (dto.state == 8) {
        // 购买充电
        return 3;
    }
    
    // 调查中 或 待支付
    return 0;
}

#pragma mark - delegate --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.styleType == BROrderDetail_Swap) {
        if (self.row == 1) {
            return 4;
        }
        return 1;
    }
    
    if (self.row == 0) {
        return 3;
    }
    
    if (self.row == 1) {
        return 2;
    }
    // row = 3 不同状态返回的个数
    if (self.state == 0) {
        return 2;
    }
    
    if (self.state == 1) {
        return 4;
    }
    if (self.state == 3) {
        return 3;
    }
    
    return 4;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.styleType == BROrderDetail_Swap) {
        
        
        BRDetailOneTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"BRDetailOneTableViewCell"];
        if (self.row == 2) {
            [cell changeStyle:0];
            cell.leftImg.image = UIImageMake(@"swap_swap");
            cell.leftImgBG.backgroundColor = UIColor.qd_swapDetailColor;
            cell.titlelLB1.text = Localized(@"swap_location");
//            cell.contentLB1.text = [self lineAtStrEmpty:self.dto.swapBranchName];
            NSString *timeStr = @"";
//            if (self.dto.swapTime.integerValue != 0) {
//                timeStr = [BRTools getTimeFromTimestamp:self.dto.swapTime.integerValue formatter:kBRDateFormatterdMy];
//            }
            cell.lineV.hidden = YES;
            cell.timeLB.text = timeStr;
        }else {
            // 样式
            [cell changeStyle:1];
            [cell singleLabel:YES];
            
            NSArray *titles = @[Localized(@"swap_recordNo"),Localized(@"swap_previousPB"),Localized(@"swap_newPB"),Localized(@"swap_stationId"),];
            cell.lineV.hidden = NO;
            if (self.row == 3) {
                titles = @[Localized(@"swap_fees")];
                NSString *symbol = USERManager.currency.uppercaseString;
                cell.contentLB1.text = [NSString stringWithFormat:@"%@%.2f",symbol,self.dto.realAmount.floatValue];
                cell.lineV.hidden = YES;
            }else {
                NSString *content = @"--";
                if (indexPath.row == 0) {
                    content = self.dto.orderNo;
                }
                else if (indexPath.row == 1) {
//                    content = [self swapAtStrEmpty:self.dto.returnPbNo];
                }
                else if (indexPath.row == 2) {
//                    content = [self swapAtStrEmpty:self.dto.swapPbNo];
                }
                else if (indexPath.row == 3) {
                    cell.lineV.hidden = YES;
//                    content = [self swapAtStrEmpty:self.dto.swapCabinetNo];
                }
                
                cell.contentLB1.text = content;
            }
            cell.titlelLB1.text = titles[indexPath.row];
            
            
        }
        
        return cell;
    }
    
    if (self.row == 2 && indexPath.row == self.state + 1) {
        BRDetailTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRDetailTwoTableViewCell"];
        // 计划
        cell.titleLB.text = Localized(@"Station_pricePlan");
        NSString *rule = USERManager.rentRule;
        cell.contentLB.text = rule;
        NSString *text = rule;
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text];
        att.yy_font = [UIFont br_fontM:14];
        att.yy_lineSpacing = 2;
        cell.contentLB.attributedText = att;
        
        return cell;
    }
    
    BRDetailOneTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"BRDetailOneTableViewCell"];
//    cell.contentView.backgroundColor = UIColor.qd_backgroundColorLighten;
    
    if (self.row == 0) {
        // 样式
        [cell changeStyle:1];
        [cell singleLabel:YES];
        // 数据
        NSString *text = Localized(@"Payment_orderId");
        NSString *contentStr = self.dto.orderNo;
        if (indexPath.row == 1) {
            text = Localized(@"Order_duration");
            if (self.dto.state == 1) {
                // 租借中使用时间计算
                contentStr = [BRTools getDurationTimeInDetail:self.dto.createTime formatter:@"yyyy年MM月dd日 HH:mm:ss"];
            }else {
                contentStr = [BRTools getFormatTimeFromDurationTime:self.dto.useDuration];
            }
        }else if (indexPath.row  == 2) {
            text = Localized(@"Order_powerBankID");
            contentStr = self.dto.pbNo;
        }
        cell.titlelLB1.text = text;
        cell.contentLB1.text = [self lineAtStrEmpty:contentStr];
        cell.lineV.hidden = NO;
        if (indexPath.row == 2) {
            cell.lineV.hidden = YES;
        }
        
    }else if (self.row == 1)  {
        [cell changeStyle:0];
        
        UIImage *img =  UIImageMake(@"order_rental");
        if ([[BRThemeManage themeName] isEqualToString:QDThemeIdentifierDark]) {
            img = UIImageMake(@"order_rental3");
        }
        UIColor *color = UIColor.qd_tintColor;
        NSString *text = Localized(@"Order_pickUp");
        NSString *contentStr = self.dto.rentBranchName;
        NSString *timeStr = @"";
        if (self.dto.createTime.integerValue != 0) {
            timeStr = [BRTools getTimeFromTimestamp:self.dto.createTime.integerValue formatter:kBRDateFormatterdMy];
        }
        if (indexPath.row == 1) {
            text = Localized(@"Order_dropOff");
            contentStr = self.dto.returnBranchName;
            
            color = UIColor.qd_tintColor2;
            img = UIImageMake(@"order_return");
//            cell.timeLB.hidden =  NO;
            if (self.dto.state == 1) {
                timeStr = @"";
            }else {
                
                timeStr = [BRTools getTimeFromTimestamp:self.dto.endTime.integerValue formatter:kBRDateFormatterdMy];
                if (contentStr.length == 0) {
                    // 没有归还地点，属于异常订单，业务不显示归还时间
                    timeStr = @"";
                }
            }
            
            if ([[BRThemeManage themeName] isEqualToString:QDThemeIdentifierDark]) {
                img = UIImageMake(@"order_return3");
            }
        }
        cell.leftImgBG.backgroundColor = color;
        cell.leftImg.image = img;
        cell.titlelLB1.text = text;
        cell.contentLB1.text = [self lineAtStrEmpty:contentStr];
        cell.timeLB.text = timeStr;
        cell.lineV.hidden = NO;
        if (indexPath.row == 1) {
            cell.lineV.hidden = YES;
        }
        
    }else {
        
        [cell changeStyle:1];
        NSString *text = Localized(@"Order_rentalAmount");
        NSString *symbol = USERManager.currency.uppercaseString;
        NSString *contentStr = [NSString stringWithFormat:@"%@%.2f",symbol,self.dto.orderAmount.floatValue];
        if (self.dto.state == 2) {
            contentStr = [NSString stringWithFormat:@"%@%.2f",symbol,self.dto.money.floatValue];
        }else if (self.dto.state  == 7) {
            contentStr = [NSString stringWithFormat:@"%@%.2f",symbol,self.dto.penaltyAmount.floatValue];
        }
        //
        [cell singleLabel:YES];
        cell.lineV.hidden = NO;
        // 购买且最后一个隐藏line
        cell.lineV.hidden = self.state == 3 && indexPath.row == 2;
        if (self.state != 0) {
            [cell singleLabel:NO];
            
            
//            NSString *text2 = Localized(@"Order_coupon");
//            NSString *contentStr2 = [NSString stringWithFormat:@"%@%.2f",symbol,self.dto.couponAmount.floatValue];
            NSString *text2 = Localized(@"Order_actualPayment");
            NSString *contentStr2 = [NSString stringWithFormat:@"%@%.2f",symbol,self.dto.realAmount.floatValue];
            
            if (indexPath.row == 1) {
//                text = Localized(@"Order_actualPayment");
//                text2 = Localized(@"Order_paymentMethod");
//                contentStr = [NSString stringWithFormat:@"%@%.2f",symbol,self.dto.realAmount.floatValue];
//                contentStr2 = [BRTools payWith:self.dto.paymentMethod];
                
                text = Localized(@"Order_coupon");
                text2 = Localized(@"Rewards_promoCode");
                contentStr = [NSString stringWithFormat:@"%@%.2f",symbol,self.dto.couponAmount.floatValue];
                contentStr2 = [NSString stringWithFormat:@"%@%.2f",symbol,self.dto.promoCodeAmount.floatValue];
            }
            
            if (indexPath.row == 2) {
//                [cell singleLabel:YES];
                if (self.dto.paymentState < 3) {
                    [cell singleLabel:YES];
                }
                text = Localized(@"Order_paymentMethod");
//                if (BR_StringIsEmpty(self.dto.paymentMethod)) {
//                    contentStr = @"--";
//                }else {
////                    contentStr = [BRTools payWith:[self.dto.paymentMethod integerValue]];
//                }
                
                text2 = Localized(@"pay_refuned");
                contentStr2 = [NSString stringWithFormat:@"%@%.2f",symbol,self.dto.refundAmount.floatValue];
            }
            
            cell.titleLB2.text = text2;
            cell.contentLB2.text = contentStr2;
            
        }
        
        cell.titlelLB1.text = text;
        cell.contentLB1.text = contentStr;
        
    }
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
}


- (NSString *)swapAtStrEmpty:(NSString *)str {
    if (str.length != 0) {
        return str;
    }
    if (self.dto.paymentState == 4 ) {
        return @"--";
    }
    return Localized(@"swap_pending");
}

- (NSString *)lineAtStrEmpty:(NSString *)str {
    if (str.length != 0) {
        return str;
    }
    return @"--";
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
